<?php
/**
	PingCo PHP Framework
	Copyright (c) 2009 - 2010 Henter.

	描述:开发基于PingCo开放平台开发第三方应用
	Author:Henter 511589173@qq.com
*/
class API {
	  public $api_key;
	  public $api_decrypt;
	  public $call_id;
	  public $sig;
	  public $session_key;
	  public $v;
	  public $method;
	  public $server;

	public function __construct($api_key='ce5e629f-3b2d-4854-b948-ad7211b5dafd',$api_decrypt='',$call_id='',$sig='',$session_key='',$v='1.1') 
	{	
		$this->api_key  = $api_key;
		$this->api_decrypt  = $api_decrypt;
		$this->call_id  = $call_id;
		$this->sig  = $sig;
		$this->session_key  = $session_key;
		$this->v = $v;
		$this->server = 'http://wap.pingco.com:8080/httpsrv2.0/api/apiserver.aspx';
	}

	/**免费发短信
	返回：['@attributes']['ret']
	*/
	public function message_send($mobile,$content='')
	{
		$method = 'message_send';
		$params = '<mobile>'.$mobile.'</mobile><content>'.$content.'</content>';
		$poststr = $this->poststr($method,$params);
		//echo $poststr;
		return $this->post_request($poststr);
	}


	/**发送在线信息
	返回：['@attributes']['ret']
	*/
	public function message_sendonline($uid,$content='',$urltitle='',$url='')
	{
		$method = 'message_sendonline';
		$params = '<uid>'.$uid.'</uid><content>'.$content.'</content><urltitle>'.$urltitle.'</urltitle><url>'.$url.'</url>';
		$poststr = $this->poststr($method,$params);
		//echo $poststr;
		return $this->post_request($poststr);
	}


	/**获取用户短信
	mobile 用户手机号
	返回：['@attributes']['ret']
	message
		fromnumber
		content
		datetime
	*/
	public function message_getlist($mobile='')
	{
		$method = 'message_getlist';
		$params = '<mobile>'.$mobile.'</mobile>';
		$poststr = $this->poststr($method,$params);
		//echo $poststr;
		return $this->post_request($poststr);
	}


	/**扣除金苹果
	money值 负数是扣 正数是奖 
	返回：['@attributes']['ret']
	*/
	public function cash_subgoldapple($money='')
	{
		$method = 'cash_subgoldapple';
		$params = '<money>'.$money.'</money>';
		$poststr = $this->poststr($method,$params);
		//echo $poststr;
		return $this->post_request($poststr);
	}



	/**购买用户信息 扣金苹果
	返回：['@attributes']['ret']
	mobile 购买用户手机号
	orderid 订单号
	*/
	public function cash_buyuserinfo($money='',$mobile='')
	{
		$method = 'cash_buyuserinfo';
		$params = '<money>'.$money.'</money><mobile>'.$mobile.'</mobile>';
		$poststr = $this->poststr($method,$params);
		//echo $poststr;
		return $this->post_request($poststr);
	}


	/**扣除苹果
	返回：['@attributes']['ret']
	*/
	public function cash_subapple($money='')
	{
		$method = 'cash_subapple';
		$params = '<money>'.$money.'</money>';
		$poststr = $this->poststr($method,$params);
		//echo $poststr;
		return $this->post_request($poststr);
	}



	/**取消金苹果订单
	返回：['@attributes']['ret']
	*/
	public function cash_cancelorder($userid='',$orderid='',$opuid='')
	{
		$method = 'cash_cancelorder';
		$params = '<userid>'.$userid.'</userid><orderid>'.$orderid.'</orderid>';
		if($opuid){
			$params .= '<opuid>'.$opuid.'</opuid>';
		}
		$poststr = $this->poststr($method,$params);
		//echo $poststr;
		return $this->post_request($poststr);
	}


	/**下发 Push信息
	返回：['@attributes']['ret']
	*/
	public function message_downloadpongco($mobile='')
	{
		$method = 'message_downloadpongco';
		$params = '<mobile>'.$mobile.'</mobile>';
		$poststr = $this->poststr($method,$params);
		//echo $poststr;
		return $this->post_request($poststr);
	}


	/**获取用户信息
	获取用户信息,如果session_key不为空代表获取登录用户的信息，如果session_key为空(session_key=""也不要写在XML中)，那么是获取uids内的用户信息，二者必先其一；
	uids可以用手机号和PingCoID
	fields值可以是uid,name,sex,birthday,height,weight,mychat,nickname,mymobile,interest,
	favoritemusic,favoritestar,favoritegame,favoritesoft,favoritecartoon,tinyurl,headurl,
	hometown_location,country,province,city,present_history,apples,goldapples,magicapples
	返回：['@attributes']['ret']
	user
		uid
		name
		sex
		...
	*/
	public function users_getinfo($uids='',$fields='',$sid='')
	{
		$method = 'users_getinfo';
		$fields = !empty($fields) ? $fields : 'uid,name,sex,mymobile,tinyurl,headurl,apples,goldapples,magicapples';
		if($fields == 'all'){
			$fields = 'uid,name,sex,birthday,height,weight,mychat,nickname,mymobile,interest,
	favoritemusic,favoritestar,favoritegame,favoritesoft,favoritecartoon,tinyurl,headurl,
	hometown_location,country,province,city,present_history,apples,goldapples,magicapples';
		}
		if(!empty($sid)){
			$params = '<request method ="'.$method.'" api_key="'.$this->api_key.'" api_decrypt="" call_id="" sig="" session_key="'.$sid.'" v="1.1" >';
			$params .= '<fields>'.$fields.'</fields>';
		}else{
			$params = '<request method ="'.$method.'" api_key="'.$this->api_key.'" api_decrypt="" call_id="" sig="" v="1.1" >';
			$params .= '<uids>'.$uids.'</uids><fields>'.$fields.'</fields>';
		}
		$params .= '</request>';
		
		$poststr = $params;
		//echo $poststr;
		return $this->post_request($poststr);
	}


	/**发布 NewsFeed
	返回：['@attributes']['ret']
	*/
	public function feed_publish($template_id='',$data='')
	{
		$method = 'feed_publish';
		$params = '<template_id>'.$template_id.'</template_id><data>'.$data.'</data>';
		$poststr = $this->poststr($method,$params);
		//echo $poststr;
		return $this->post_request($poststr);
	}


	/**获取好友 ID列表
	返回：['@attributes']['ret']
	uid
	*/
	public function friends_get($uid='')
	{
		$method = 'friends_get';
		$params = '<uid>'.$uid.'</uid>';
		$poststr = $this->poststr($method,$params);
		//echo $poststr;
		return $this->post_request($poststr);
	}


	/**获取好友列表(ID、名称、大小图) 
	返回：['@attributes']['ret']
	friend
		id
		name
		headurl
		headbigurl
	*/
	public function friends_getfriends($uid='')
	{
		$method = 'friends_getfriends';
		$params = '<uid>'.$uid.'</uid>';
		$poststr = $this->poststr($method,$params);
		//return $poststr;
		return $this->post_request($poststr);
	}


	/**邀请好友
	返回：['@attributes']['ret']
	*/
	public function invite_friends($friends='',$inviteid='')
	{
		$method = 'invite_friends';
		$params = '<friends>'.$friends.'</friends><inviteid>'.$inviteid.'</inviteid>';
		$poststr = $this->poststr($method,$params);
		//echo $poststr;
		return $this->post_request($poststr);
	}


	/**邀请所有好友
	返回：['@attributes']['ret']
	*/
	public function invite_allfriends($inviteid='')
	{
		$method = 'invite_allfriends';
		$params = '<inviteid>'.$inviteid.'</inviteid>';
		$poststr = $this->poststr($method,$params);
		//echo $poststr;
		return $this->post_request($poststr);
	}



	 public function post_request($params)
	 {
		$post_string = $params;
		if (function_exists('curl_init')) {
		  // Use CURL if installed...
		  $ch = curl_init();
		  curl_setopt($ch, CURLOPT_URL, $this->server);
		  curl_setopt($ch, CURLOPT_POST, 1);
		  curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
		  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		  //curl_setopt($ch, CURLOPT_USERAGENT, 'PingCo API PHP5 Client 1.0 (curl) ' . phpversion());
		  $result = curl_exec($ch);
		  curl_close($ch);
		} else {
		 // Non-CURL based version...
		  $context =
			array('http' =>
				  array('method' => 'POST',
						'header' => 'Content-type: application/x-www-form-urlencoded'."\r\n".
									'User-Agent: Facebook API PHP5 Client 1.1 (non-curl) '.phpversion()."\r\n".
									'Content-length: ' . strlen($post_string),
						'content' => $post_string));
		  $contextid=stream_context_create($context);

		  $sock=fopen($this->server, 'r', false, $contextid);
		  if ($sock) {
			$result='';
			while (!feof($sock))
			  $result.=fgets($sock, 4096);
			fclose($sock);
		  }
		}
	$result = $this->xml_to_array($result);
		$this->checkreturn($result);
		return $result;
	return $result;
	 }
	 

	private function xml_to_array($xml)
	{
		$array = (array)(simplexml_load_string($xml));
		foreach ($array as $key=>$item){
			$array[$key]  = $this->struct_to_array((array)$item);
		 }
		return $array;
		}

	private	function struct_to_array($item)
	{
		  if(!is_string($item)) {
			$item = (array)$item;
			foreach ($item as $key=>$val){
			  $item[$key]  =  self::struct_to_array($val);
			}
		  }
		  return $item;
		}


	private function checkreturn($result)
	{
		$err = '';
		$ret = !empty($result['@attributes']['ret']) ? $result['@attributes']['ret'] : '';
		$msg = !empty($result['@attributes']['msg']) ? $result['@attributes']['msg'] : '';
		if($msg)
		{
			$err='<p>访问出错!</p>';
			if($msg)
			{
				$err.='错误信息:'.$msg.'<br>';
			}
		}
		//if($ret==0){echo($err);}
	}


	public function poststr($method,$params)
	{
		global $sid; 
		return '<request method ="'.$method.'" api_key="'.$this->api_key.'" api_decrypt="" call_id="" sig="" session_key="'.$sid.'" v="1.1" >'.$params.'</request>';

	}

}
